import Vue from 'vue'
import VueRouter from 'vue-router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { Message } from 'element-ui'

import login from '@/views/login'
import layout from '@/views/layout/layout'
import chart from '@/views/layout/chart/chart'
import users from '@/views/layout/users/users'
import question from '@/views/layout/question/question'
import business from '@/views/layout/business/business'
import subject from '@/views/layout/subject/subject'
import store from '@/store'
import {removeLocal} from '@/utils/local.js'



Vue.use(VueRouter);

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: login,
            meta: {
                title: '登录',
                roles: ['超级管理员', '管理员', '老师', '学生']
            }
        },
        {
            path: '/layout',
            component: layout,
            redirect: '/layout/subject',
            children: [
                {
                    path: '/layout/chart',
                    icon: 'el-icon-pie-chart',
                    component: chart,
                    meta: {
                        title: '数据概览',
                        roles: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: '/layout/users',
                    icon: 'el-icon-user',
                    component: users,
                    meta: {
                        title: '用户列表',
                        roles: ['超级管理员', '管理员']
                    }
                },
                {
                    path: '/layout/question',
                    icon: 'el-icon-pie-chart',
                    component: question,
                    meta: {
                        title: '题库列表',
                        roles: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: '/layout/business',
                    icon: 'el-icon-office-building',
                    component: business,
                    meta: {
                        title: '企业列表',
                        roles: ['超级管理员', '管理员', '老师']
                    }
                },
                {
                    path: '/layout/subject',
                    icon: 'el-icon-notebook-2',
                    component: subject,
                    meta: {
                        title: '学科列表',
                        roles: ['超级管理员', '管理员', '老师', '学生']
                    }
                },
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    Nprogress.start();

    // 权限允许就跳转
    if (to.meta.roles.includes(store.state.role)) {
        next();
    } else {
        Message.error('您无权访问该页面');
        removeLocal('token');
        next('/login');
    }
})

router.afterEach((to) => {
    document.title = '黑马面面-' + to.meta.title;
    Nprogress.done();
})

const originalReplace = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalReplace.call(this, location).catch(err => err)
}
export default router


